//
//  BKStatusTableViewCell.swift
//  微博
//
//  Created by mac on 16/8/12.
//  Copyright © 2016年 xbk. All rights reserved.
/*
"<SnapKit.LayoutConstraint:0x7fa49cbccc30@/Users/mac/Desktop/hello word/swift/day 04/\U5fae\U535a/weibo/\U5fae\U535a/\U5fae\U535a/Classes/View/Home/View/BKStatusTableViewCell.swift#102 \U00c2\U00e6\U00c6\U00c2\U00e7\U00f6.BKStatusToolBar:0x7fa49cbbd620.top == \U00c2\U00e6\U00c6\U00c2\U00e7\U00f6.BKStatusRetweetView:0x7fa49cbc56f0.bottom>",
"<SnapKit.LayoutConstraint:0x7fa49cbd5d40@/Users/mac/Desktop/hello word/swift/day 04/\U5fae\U535a/weibo/\U5fae\U535a/\U5fae\U535a/Classes/View/Home/View/BKStatusTableViewCell.swift#39 \U00c2\U00e6\U00c6\U00c2\U00e7\U00f6.BKStatusToolBar:0x7fa49cbbd620.top == \U00c2\U00e6\U00c6\U00c2\U00e7\U00f6.BKStatusOriginView:0x7fa49cba5260.bottom>"
约束冲突
创建约束的时候需要把以前的约束删除
*/

import UIKit
import SnapKit

class BKStatusTableViewCell: UITableViewCell {
    
    //toolbar 底部约束记录
    var toolBarTopConstraint:Constraint?
    
    //处理微博数据的viewModel
    var statusViewModel :BKStatusViewModel?{
    
        didSet{
            
            originView.statusViewModel = statusViewModel
            toolBar.statusViewModel = statusViewModel
            print(statusViewModel?.status?.retweeted_status?.text)
            
            //卸载上一次的约束
            toolBarTopConstraint?.uninstall()
            
            //判断是否有转发微博对象
            if statusViewModel?.status?.retweeted_status != nil {
            
                //显示转发微博的视图
                retweetView.hidden = false
                //更新约束
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                   self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            
                retweetView.statusViewModel = statusViewModel
            }else{
            
                //隐藏转发微博的视图
                retweetView.hidden = true
                
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                   self.toolBarTopConstraint = make.top.equalTo(originView.snp_bottom).constraint
                })
            }
        }
        
    }
    
    //  MARK: --  懒加载控件
    private lazy var originView:BKStatusOriginView = {
    
        let view = BKStatusOriginView ()
        //view.backgroundColor = RandomColor()
        return view
    }()
    
    //转发微博
    private lazy var retweetView:BKStatusRetweetView = {
    
        let view = BKStatusRetweetView()
        //view.backgroundColor = RandomColor()
        return view
    }()
    
    
    //toolbar 
    private lazy var toolBar:BKStatusToolBar = BKStatusToolBar()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //添加控件,设置约束
    private func setupUI () {
    
        //添加控件
        contentView.addSubview(originView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)
        
        //设置约束
        originView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentView)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originView.snp_bottom)
            make.leading.equalTo(originView)
            make.trailing.equalTo(originView)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
            
        }
        
        //关键约束
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(toolBar)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
        }
        
    }

}
